Enter the directory of the maca folder on your drive and the name of the tissue you want to analyze.
tissue_of_interest = "Spleen"
Warning message:
package ‘Seurat’ was built under R version 3.4.3
library(here)
here() starts at /Users/olgabot/code/tabula-muris
source(here("00_data_ingest", "02_tissue_analysis_rmd", "boilerplate.R"))
package ‘dplyr’ was built under R version 3.4.2
Attaching package: ‘dplyr’
The following objects are masked from ‘package:stats’:
filter, lag
The following objects are masked from ‘package:base’:
intersect, setdiff, setequal, union
package ‘tidyverse’ was built under R version 3.4.2── Attaching packages ──────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
✔ tibble 1.3.4 ✔ purrr 0.2.4
✔ tidyr 0.7.2 ✔ stringr 1.2.0
✔ readr 1.1.1 ✔ forcats 0.2.0
package ‘tidyr’ was built under R version 3.4.2package ‘purrr’ was built under R version 3.4.2── Conflicts ─────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
✖ tidyr::expand() masks Matrix::expand()
✖ dplyr::filter() masks stats::filter()
✖ cowplot::ggsave() masks ggplot2::ggsave()
✖ dplyr::lag() masks stats::lag()
tiss = load_tissue_facs(tissue_of_interest)
Performing log-normalization
0% 10 20 30 40 50 60 70 80 90 100%
[----|----|----|----|----|----|----|----|----|----|
**************************************************|
[1] "Scaling data matrix"
|
| | 0%
|
|===========================================================================================================================| 100%
Calculating gene means
0% 10 20 30 40 50 60 70 80 90 100%
[----|----|----|----|----|----|----|----|----|----|
**************************************************|
Calculating gene variance to mean ratios
0% 10 20 30 40 50 60 70 80 90 100%
[----|----|----|----|----|----|----|----|----|----|
**************************************************|
Visualize top genes in principal components
Later on (in FindClusters and TSNE) you will pick a number of principal components to use. This has the effect of keeping the major directions of variation in the data and, ideally, supressing noise. There is no correct answer to the number to use, but a decent rule of thumb is to go until the plot plateaus.
PCElbowPlot(object = tiss)
Choose the number of principal components to use.
# Set number of principal components.
n.pcs = 7
The clustering is performed based on a nearest neighbors graph. Cells that have similar expression will be joined together. The Louvain algorithm looks for groups of cells with high modularity–more connections within the group than between groups. The resolution parameter determines the scale…higher resolution will give more clusters, lower resolution will give fewer.
For the top-level clustering, aim to under-cluster instead of over-cluster. It will be easy to subset groups and further analyze them below.
# Set resolution
res.used <- .5
tiss <- FindClusters(object = tiss, reduction.type = "pca", dims.use = 1:n.pcs,
resolution = res.used, print.output = 0, save.SNN = TRUE)
Build parameters exactly match those of already computed and stored SNN. To force recalculation, set force.recalc to TRUE.
To visualize
# If cells are too spread out, you can raise the perplexity. If you have few cells, try a lower perplexity (but never less than 10).
tiss <- RunTSNE(object = tiss, dims.use = 1:n.pcs, seed.use = 10, perplexity=20)
# note that you can set do.label=T to help label individual clusters
TSNEPlot(object = tiss, do.label = T)
Compare to previous annotations
filename = here('00_data_ingest', '03_tissue_annotation_csv',
paste0(tissue_of_interest, "_facs_annotation.csv"))
previous_annotation = read_csv(filename)
Missing column names filled in: 'X1' [1]Parsed with column specification:
cols(
X1 = col_character(),
plate.barcode = col_character(),
cell_ontology_class = col_character(),
cell_ontology_id = col_character(),
free_annotation = col_character(),
tSNE_1 = col_double(),
tSNE_2 = col_double()
)
tiss@meta.data[, 'previous_annotation'] <- "NA"
tiss@meta.data[as.character(previous_annotation$X1), 'previous_annotation'] <- as.character(previous_annotation$cell_ontology_class)
TSNEPlot(object = tiss, do.return = TRUE, group.by = "previous_annotation")
table(tiss@meta.data[, "previous_annotation"])
B cell Macrophage NA T cell
1291 50 8 348
table(tiss@meta.data[, "previous_annotation"], tiss@ident)
0 1 2 3 4 5
B cell 663 3 323 254 48 0
Macrophage 0 0 0 2 0 48
NA 1 2 5 0 0 0
T cell 0 347 1 0 0 0
Check expression of genes of interset.
Dotplots let you see the intensity of exppression and the fraction of cells expressing for each of your genes of interest.
How big are the clusters?
table(tiss@ident)
0 1 2 3 4 5
664 352 329 256 48 48
At a coarse level, we can use canonical markers to match the unbiased clustering to known cell types:
# stash current cluster IDs
tiss <- StashIdent(object = tiss, save.name = "cluster.ids")
Color by metadata, like plate barcode, to check for batch effects.
Print a table showing the count of cells in each identity category from each plate.
tiss <- RunPCA(object = tiss, do.print = FALSE)
tiss <- ProjectPCA(object = tiss, do.print = FALSE)
When you save the annotated tissue, please give it a name.
filename = here('00_data_ingest', '04_tissue_robj_generated',
paste0("facs", tissue_of_interest, "_seurat_tiss.Robj"))
print(filename)
[1] "/Users/olgabot/code/tabula-muris/00_data_ingest/04_tissue_robj_generated/facsSpleen_seurat_tiss.Robj"
save(tiss, file=filename)
# To reload a saved object
# filename = here('00_data_ingest', '04_tissue_robj_generated',
# paste0("facs", tissue_of_interest, "_seurat_tiss.Robj"))
# load(file=filename)
So that Biohub can easily combine all your cell_ontology_classs, please export them as a simple csv.
head(tiss@meta.data)
filename = here('00_data_ingest', '03_tissue_annotation_csv',
paste0(tissue_of_interest, "_facs_annotation.csv"))
write.csv(FetchData(tiss, c('plate.barcode','cell_ontology_class','cell_ontology_id', 'free_annotation', 'tSNE_1', 'tSNE_2')), file=filename)